﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;

namespace LiWiTools
{
    public class DataAxis
    {
        /// <summary>
        /// 初始化数据
        /// </summary>
        public  bool iniFlag = false;
        /// <summary>
        /// 轴类型
        /// </summary>
        public  int[] axisType;
        /// <summary>
        /// 脉冲当量
        /// </summary>
        public  float[] units;
        /// <summary>
        /// 起始速度
        /// </summary>
        public  float[] startSpeed;
        /// <summary>
        /// 运行速度
        /// </summary>
        public  float[] runSpeed;
        /// <summary>
        /// 加速度
        /// </summary>
        public  float[] accel;
        /// <summary>
        /// 减速度
        /// </summary>
        public  float[] decel;
        /// <summary>
        /// 快速减速度
        /// </summary>
        public  float[] fastDec;
        /// <summary>
        /// 正向软限位
        /// </summary>
        public  float[] fsLimit;
        /// <summary>
        /// 负向软限位
        /// </summary>
        public  float[] rsLimit;
        /// <summary>
        /// 原点IO
        /// </summary>
        public  int[] datumIO;
        /// <summary>
        /// 正限位IO
        /// </summary>
        public  int[] fwdIO;
        /// <summary>
        /// 负限位IO
        /// </summary>
        public  int[] revIO;
        /// <summary>
        /// 脉冲位置
        /// </summary>
        public float[] dPos;
        /// <summary>
        /// 编码器位置
        /// </summary>
        public float[] mPos;
        /// <summary>
        /// 停止状态
        /// </summary>
        public int[] idle;
        /// <summary>
        /// 当前运动类型
        /// </summary>
        public int[] mtype;


        /// <summary>
        /// 报警信息
        /// </summary>
        public  Dictionary<int, string> errCodeDesc = new Dictionary<int, string>();
        /// <summary>
        /// 报警轴
        /// </summary>
        public  string[] errAxisMes;



        /// <summary>
        /// 回原模式
        /// </summary>
        public  int _model;
        /// <summary>
        /// 限位反找开关:1开启
        /// </summary>
        public  int _goBackModel;
        /// <summary>
        /// 正硬限位IO
        /// </summary>
        public  int _hardFwdIN;
        /// <summary>
        /// 负硬限位IO
        /// </summary>
        public  int _hardRevIN;
        /// <summary>
        /// 原点IO
        /// </summary>
        public  int _homeIN;
        /// <summary>
        /// 反找等待时间
        /// </summary>
        public  int _waitCT;
        /// <summary>
        /// 偏移距离
        /// </summary>
        public  float _pianyi;
        /// <summary>
        /// 爬行速度
        /// </summary>
        public  float _creep;



        /// <summary>
        /// 轴参数获取
        /// </summary>
        public  void GetAxisData(Data IPName,DataControl dataControl)
        {
            if (iniFlag)
            {
                for (int i = 0; i < dataControl.axisVirtuAxises; i++)
                {
                    PMC_GetAtype(IPName.handle, i, ref axisType[i]);      //轴类型
                    PMC_GetUnits(IPName.handle, i, ref units[i]);           //脉冲当量
                    PMC_GetLspeed(IPName.handle, i, ref startSpeed[i]);     //起始速度
                    PMC_GetSpeed(IPName.handle, i, ref runSpeed[i]);      //运行速度
                    PMC_GetAccel(IPName.handle, i, ref accel[i]);         //加速度
                    PMC_GetDecel(IPName.handle, i, ref decel[i]);         //减速度
                    PMC_GetFastDec(IPName.handle, i, ref fastDec[i]);     //快速减速度
                    PMC_GetFsLimit(IPName.handle, i, ref fsLimit[i]);     //正向软限位
                    PMC_GetRsLimit(IPName.handle, i, ref rsLimit[i]);     //负向软限位
                    PMC_GetDatumIn(IPName.handle, i, ref datumIO[i]);     //原点IO
                    PMC_GetFwdIn(IPName.handle, i, ref fwdIO[i]);         //正限位IO
                    PMC_GetRevIn(IPName.handle, i, ref revIO[i]);         //负限位IO
                    PMC_GetDpos(IPName.handle, i, ref dPos[i]);
                    PMC_GetMpos(IPName.handle, i, ref mPos[i]);
                    PMC_GetIfIdle(IPName.handle, i, ref idle[i]);
                    PMC_GetMtype(IPName.handle, i, ref mtype[i]);

                    //报警信息
                    int errorNum = 0;
                    PMC_GetAxisStatus(IPName.handle, i, ref errorNum);
                    if (errorNum != 0)
                    {
                        for (int j = 1; j < 24; ++j)
                        {
                            if ((j > 0 && j < 6) || (j > 7 && j < 13) || j == 22 || j == 23)
                            {
                                if (IPName.GetBitValue(errorNum, (ushort)j))
                                {   //有报警
                                    errAxisMes[i] = errCodeDesc[j];
                                }
                            }
                        }
                    }
                    else errAxisMes[i] = "无报警";

                    //Thread.Sleep(20);
                }
            }
            else
            {
                axisType = new int[dataControl.axisVirtuAxises];
                units = new float[dataControl.axisVirtuAxises];
                startSpeed = new float[dataControl.axisVirtuAxises];
                runSpeed = new float[dataControl.axisVirtuAxises];
                accel = new float[dataControl.axisVirtuAxises];
                decel = new float[dataControl.axisVirtuAxises];
                fastDec = new float[dataControl.axisVirtuAxises];
                fsLimit = new float[dataControl.axisVirtuAxises];
                rsLimit = new float[dataControl.axisVirtuAxises];
                datumIO = new int[dataControl.axisVirtuAxises];
                fwdIO = new int[dataControl.axisVirtuAxises];
                revIO = new int[dataControl.axisVirtuAxises];
                errAxisMes = new string[dataControl.axisVirtuAxises];
                dPos = new float[dataControl.axisVirtuAxises];
                mPos = new float[dataControl.axisVirtuAxises];
                idle = new int[dataControl.axisVirtuAxises];
                mtype = new int[dataControl.axisVirtuAxises];

                //添加错误信息
                errCodeDesc.Add(1, "随动误差超限告警\n");
                errCodeDesc.Add(2, "与远程轴通讯出错\n");
                errCodeDesc.Add(3, "远程驱动器报错\n");
                errCodeDesc.Add(4, "正向硬限位\n");
                errCodeDesc.Add(5, "反向硬限位\n");
                errCodeDesc.Add(6, "找原点中\n");
                errCodeDesc.Add(8, "随动误差超限出错\n");
                errCodeDesc.Add(9, "超过正向软限位\n");
                errCodeDesc.Add(10, "超过负向软限位\n");
                errCodeDesc.Add(11, "CANCEL执行中\n");
                errCodeDesc.Add(12, "脉冲频率超过MAX_SPEED限制.需要修改降速或修改MAX_SPEED\n");
                errCodeDesc.Add(22, "告警信号输入\n");
                errCodeDesc.Add(23, "轴进入了暂停状态\n");


                iniFlag = true;
            }
        }
    }

}
